# -*- coding:utf-8 -*-
from tornado import web, gen
import os, base64

from appcommon.framework import APIRequestHandler
from appcommon import checktool
from common import utiltool
import models as couchm
from user import models as userm
from circle import models as circlem
from common import timetool
import settings
from pay import models as paym


class GetSchoolHandler(APIRequestHandler):
    """获取驾校列表"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        area = self.get_argument('area', '')
        info = yield couchm.get_school_list(area)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class SetBaseInfoHandler(APIRequestHandler):
    """设置教练基本信息"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        nick_name = self.get_argument('nick_name', '')
        sex = self.get_argument('sex', '') # female male
        tel = self.get_argument('tel', '')
        years = self.get_argument('years', '')
        car_type = self.get_argument('car_type', '')
        current_time = timetool.get_timestamp()
        yield couchm.set_couch_base_info(phone, nick_name, sex, tel, years, car_type)
        yield couchm.set_couch_avatar_nick(phone, nick_name, '', current_time)
        res = {"code": "0", "msg": u"设置成功"}
        utiltool.response_write(self, res)


class SetCouchAvatarHandler(APIRequestHandler):
    """设置教练头像"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        base64str = self.get_argument('avatar', '')
        current_time = timetool.get_timestamp()
        file_type = base64str.split(';')[0].split('/')[-1]
        avatar = base64str.split(';')[1].split(',')[-1]
        if avatar:
            upload_path = settings.IMG_BASE_PATH + 'avatar/'
            os.path.exists('upload_path')
            filename = str(phone) + str(current_time) + '.' + file_type
            filepath = os.path.join(upload_path, filename)
            avatar_url = settings.IMG_BASE_URL + 'avatar/{}'.format(filename)
            img_data = base64.b64decode(avatar)
            with open(filepath, 'wb') as up:  # 有些文件需要已二进制的形式存储，实际中可以更改
                up.write(img_data)
        else:
            avatar_url = ""
        yield couchm.set_couch_avatar_nick(phone, '', avatar_url, current_time)
        res = {"code": "0", "avatar": avatar_url}
        utiltool.response_write(self, res)


class SetDriverAddrHandler(APIRequestHandler):
    """设置练车地址"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        area_str = self.get_argument('area_str', '')
        longitude = self.get_argument('longitude', '')
        latitude = self.get_argument('latitude', '')
        area = self.get_argument('area', '')
        yield couchm.set_couch_drive_addr(phone, area_str, longitude, latitude, area)
        res = {"code": "0", "msg": u"设置成功"}
        utiltool.response_write(self, res)


class SetClassInfoHandler(APIRequestHandler):
    """设置班型信息"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        class_id = self.get_argument('class_id', '')
        class_name = self.get_argument('class_name', '')
        car_type = self.get_argument('car_type', '')
        price = self.get_argument('price', '')
        desc = self.get_argument('desc', '')
        yield couchm.set_class_info(phone, class_id, class_name, car_type, int(price), desc)
        res = {"code": "0", "msg": u"设置成功"}
        utiltool.response_write(self, res)


class DelClassInfoHandler(APIRequestHandler):
    """删除班型信息"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        class_id = self.get_argument('class_id', '')
        yield couchm.del_class_info(class_id)
        res = {"code": "0", "msg": u"删除成功"}
        utiltool.response_write(self, res)


class SetIntroduceHandler(APIRequestHandler):
    """设置招生特色"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        days = self.get_argument('days', '')
        points = self.get_argument('points', '')
        introduce = self.get_argument('introduce', '')
        yield couchm.set_couch_introduce(phone, days, points, introduce)
        res = {"code": "0", "msg": u"设置成功"}
        utiltool.response_write(self, res)


class UpdateAuthPicHandler(APIRequestHandler):
    """上传教练认证图片"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        print self.request.arguments
        phone = self.get_argument('phone', '')
        jlz64 = self.get_argument('jlz', '')
        sfz64 = self.get_argument('sfz', '')
        xsz64 = self.get_argument('xsz', '')
        jsz64 = self.get_argument('jsz', '')
        upload_path = settings.IMG_BASE_PATH + 'couchauth/'
        os.path.exists('upload_path')
        jlz_url = ""
        sfz_url = ""
        xsz_url = ""
        jsz_url = ""
        if jlz64:
            jlz_type = jlz64.split(';')[0].split('/')[-1]
            jlz_base64_str = jlz64.split(';')[1].split(',')[-1]
            jlz_filename = str(phone) + '_jlz' + '.' + jlz_type
            jlz_filepath = os.path.join(upload_path, jlz_filename)
            jlz_url = settings.IMG_BASE_URL + 'couchauth/{}'.format(jlz_filename)
            jlz_data = base64.b64decode(jlz_base64_str)
            with open(jlz_filepath, 'wb') as up:  # 有些文件需要已二进制的形式存储，实际中可以更改
                up.write(jlz_data)
        if sfz64:
            sfz_type = sfz64.split(';')[0].split('/')[-1]
            sfz_base64_str = sfz64.split(';')[1].split(',')[-1]
            sfz_filename = str(phone) + '_sfz' + '.' + sfz_type
            sfz_filepath = os.path.join(upload_path, sfz_filename)
            sfz_url = settings.IMG_BASE_URL + 'couchauth/{}'.format(sfz_filename)
            sfz_data = base64.b64decode(sfz_base64_str)
            with open(sfz_filepath, 'wb') as up:  # 有些文件需要已二进制的形式存储，实际中可以更改
                up.write(sfz_data)
        if xsz64:
            xsz_type = xsz64.split(';')[0].split('/')[-1]
            xsz_base64_str = xsz64.split(';')[1].split(',')[-1]
            xsz_filename = str(phone) + '_xsz' + '.' + xsz_type
            xsz_filepath = os.path.join(upload_path, xsz_filename)
            xsz_url = settings.IMG_BASE_URL + 'couchauth/{}'.format(xsz_filename)
            xsz_data = base64.b64decode(xsz_base64_str)
            with open(xsz_filepath, 'wb') as up:  # 有些文件需要已二进制的形式存储，实际中可以更改
                up.write(xsz_data)
        if jsz64:
            jsz_type = jsz64.split(';')[0].split('/')[-1]
            jsz_base64_str = jsz64.split(';')[1].split(',')[-1]
            jsz_filename = str(phone) + '_jsz' + '.' + jsz_type
            jsz_filepath = os.path.join(upload_path, jsz_filename)
            jsz_url = settings.IMG_BASE_URL + 'couchauth/{}'.format(jsz_filename)
            jsz_data = base64.b64decode(jsz_base64_str)
            with open(jsz_filepath, 'wb') as up:  # 有些文件需要已二进制的形式存储，实际中可以更改
                up.write(jsz_data)
        yield couchm.set_couch_auth_pic(phone, jlz_url, sfz_url, xsz_url, jsz_url)
        res = {"code": "0", "info": {
            "jlz_url": jlz_url,
            "sfz_url": sfz_url,
            "xsz_url": xsz_url,
            "jsz_url": jsz_url
        }}
        utiltool.response_write(self, res)


class GetAuthPicHandler(APIRequestHandler):
    """获取教练认证图片"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        info = yield couchm.get_couch_auth_pic(phone)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class CreateCardHandler(APIRequestHandler):
    """生成名片"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        has_card = yield couchm.check_couch_has_card(phone)
        if has_card:
            res = {"code": "2", "msg": u"名片生成中，请稍候"}
        else:
            info = yield couchm.check_couch_can_create_card(phone)
            if info:
                yield couchm.add_couch_score(phone, u'生成名片', 'card', 20)
                yield couchm.create_couch_card(phone)
                res = {"code": "0", "msg": u"设置成功"}
            else:
                res = {"code": "1", "msg": u"请完善名片信息"}
        utiltool.response_write(self, res)


class ConsultHandler(APIRequestHandler):
    """报名咨询"""

    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        class_id = self.get_argument('class_id', '')
        couch_phone = self.get_argument('couch_phone', '')
        name = self.get_argument('name', '')
        tel = self.get_argument('tel', '')
        addr = self.get_argument('addr', '')
        content = self.get_argument('content', '')
        info = yield couchm.save_student_consult(phone, couch_phone, class_id, name, tel, addr, content)
        info_id = yield couchm.get_last_consult_with_phone(phone, couch_phone)
        if info_id:
            yield circlem.add_psuh_task(5, info_id['id'])
        res = {"code": "0", "msg": u"提交成功"}
        utiltool.response_write(self, res)


class PubConsultHandler(APIRequestHandler):
    """一键咨询"""

    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        car_type = self.get_argument('car_type', '')
        name = self.get_argument('name', '')
        tel = self.get_argument('tel', '')
        addr = self.get_argument('addr', '')
        content = self.get_argument('content', '')
        area = self.get_argument('area', '')
        info = yield couchm.save_pub_consult(phone, car_type, name, tel, addr, content, area)
        info_id = yield couchm.get_last_pub_consult_with_phone(phone)
        if info_id:
            yield circlem.add_psuh_task(6, info_id['id'])
        res = {"code": "0", "msg": "提交成功"}
        utiltool.response_write(self, res)


class FollowHandler(APIRequestHandler):
    """关注取消关注"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        print self.request.arguments
        phone = self.get_argument('phone', '')
        couch_phone = self.get_argument('couch_phone', '')
        set = self.get_argument('set', '1')
        is_follow = yield couchm.check_is_follow(phone, couch_phone)
        # 关注
        if set == '1':
            if is_follow:
                res = {"code": "1", "msg": u"您已经关注该教练"}
            else:
                yield couchm.follow_couch(phone, couch_phone)
                followed = yield couchm.chech_student_followed(phone, couch_phone)
                if not followed:
                    yield couchm.add_couch_score(couch_phone, phone, 'follow', 5)
                res = {"code": "0", "msg": u"关注成功"}
        # 取消关注
        else:
            if is_follow:
                yield couchm.cancel_follow_couch(phone, couch_phone)
                res = {"code": "0", "msg": u"取消关注成功"}
            else:
                res = {"code": "1", "msg": u"您未关注该教练"}
        utiltool.response_write(self, res)


class EvaluateHandler(APIRequestHandler):
    """学员评价"""

    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        couch_phone = self.get_argument('couch_phone', '')
        stars = self.get_argument('stars', '')
        content = self.get_argument('content', '')
        is_evaluate = yield couchm.check_evalueate(phone, couch_phone)
        if not is_evaluate:
            yield couchm.evaluate_couch(phone, couch_phone, stars, content)
            if stars == '5':
                yield couchm.add_couch_score(couch_phone, phone, 'evaluate', 5)
            elif stars == '4':
                yield couchm.add_couch_score(couch_phone, phone, 'evaluate', 3)
            elif stars == '3':
                yield couchm.add_couch_score(couch_phone, phone, 'evaluate', 1)
            res = {"code": "0", "msg": u"评价成功"}
        else:
            res = {"code": "1", "msg": u"已经评价过该教练"}
        utiltool.response_write(self, res)


class GetBaseInfoHandler(APIRequestHandler):
    """获取教练基本信息"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        info = yield couchm.get_couch_base_info(phone)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class GetDriverAddrHandler(APIRequestHandler):
    """获取练车地址"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        info = yield couchm.get_driver_addr(phone)
        print info
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class SetAddrPicsHandler(APIRequestHandler):
    """设置练车场地图片"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        print self.request.arguments
        phone = self.get_argument('phone', '')
        pic_url = self.get_arguments('pic_url')
        pic_b64 = self.get_arguments('pic_b64')
        current_time = timetool.get_timestamp()
        upload_path = settings.IMG_BASE_PATH + 'driveraddr/'
        for i, item in enumerate(pic_b64):
            pic_type = item.split(';')[0].split('/')[-1]
            pic_base64_str = item.split(';')[1].split(',')[-1]
            pic_filename = str(phone) + '{}{}'.format(current_time, i) + '.' + pic_type
            pic_filepath = os.path.join(upload_path, pic_filename)
            url = settings.IMG_BASE_URL + 'driveraddr/{}'.format(pic_filename)
            pic_data = base64.b64decode(pic_base64_str)
            with open(pic_filepath, 'wb') as up:  # 有些文件需要已二进制的形式存储，实际中可以更改
                up.write(pic_data)
            pic_url.append(url)
        yield couchm.set_driver_addr_pics(phone, pic_url)
        res = {"code": "0", "info": pic_url}
        utiltool.response_write(self, res)


class SetAddrPicsAndroidHandler(APIRequestHandler):
    """安卓设置练车场地图片"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        pic_url = self.get_argument('pic_url')
        old_pic_list = pic_url.split(',')
        current_time = timetool.get_timestamp()
        upload_path = settings.IMG_BASE_PATH + 'driveraddr/'
        images = []
        if self.request.files:
            file_metas = self.request.files['file']
            for i, meta in enumerate(file_metas):
                os.path.exists('upload_path')
                filename = str(phone) + str(current_time) + '_' + str(i) + '.' + meta['filename'].split('.')[-1]
                filepath = os.path.join(upload_path, filename)
                with open(filepath, 'wb') as up:  # 有些文件需要已二进制的形式存储，实际中可以更改
                    up.write(meta['body'])
                images.append(settings.IMG_BASE_URL + 'driveraddr/{}'.format(filename))
        all_pic = images + old_pic_list
        yield couchm.set_driver_addr_pics(phone, all_pic)
        res = {"code": "0", "info": all_pic}
        utiltool.response_write(self, res)


class GetClassInfoHandler(APIRequestHandler):
    """获取班型信息"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        info = yield couchm.get_class_info_list(phone)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class GetIntroduceHandler(APIRequestHandler):
    """获取招生特色"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        info = yield couchm.get_couch_introduce(phone)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class TjCouchListHandler(APIRequestHandler):
    """首页教练列表"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        area = self.get_argument('area', '')
        preference = self.get_argument('preference', '')
        car_type = self.get_argument('car_type', '')
        longitude = self.get_argument('longitude', '')  # 经度
        latitude = self.get_argument('latitude', '')  # 纬度
        # 默认  按积分
        if preference == u'口碑好':
            info = yield couchm.get_couch_list(area, car_type, 'stars desc', 5)
        elif preference == u'练车近':
            info = yield couchm.get_close_couch_list(area, longitude, latitude, car_type)
        elif preference == u'学费低':
            info = yield couchm.get_couch_list(area, car_type, 'price', 5)
        elif preference == u'人气高':
            info = yield couchm.get_couch_list(area, car_type, 'followers desc', 5)
        else:
            info = yield couchm.get_couch_list(area, car_type, 'score desc', 5)

        for item in info:
            if not preference == u'练车近':
                if longitude and latitude:
                    distance = yield couchm.get_distance_with_phone(item['phone'], longitude, latitude)
                else:
                    distance = -1
                item['distance'] = distance
            item['auth'] = yield couchm.get_couch_auth_status(item['phone'])
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class CouchListHandler(APIRequestHandler):
    """全部教练列表"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        area = self.get_argument('area', '')
        longitude = self.get_argument('longitude', '')  # 经度
        latitude = self.get_argument('latitude', '')  # 纬度
        page = self.get_argument('page', '1')  #
        page_count = self.get_argument('page_count', '20')  #
        start = (int(page) - 1) * int(page_count)
        end = int(page_count) * int(page)
        info_all = yield couchm.get_couch_list(area, '', 'score desc', 0)
        info = info_all[start: end]
        for item in info:
            if longitude and latitude:
                distance = yield couchm.get_distance_with_phone(item['phone'], longitude, latitude)
            else:
                distance = -1
            item['distance'] = distance
            item['auth'] = yield couchm.get_couch_auth_status(item['phone'])
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class MyFollowerHandler(APIRequestHandler):
    """我的学员"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        info = yield couchm.get_my_follower(phone)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class MyEvaluateHandler(APIRequestHandler):
    """我的评价"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        page_count = self.get_argument('page_count', '20')  #
        add_time = self.get_argument('add_time', '')  #

        info = yield couchm.get_my_evaluate(phone, page_count, add_time)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class PubConsultListHandler(APIRequestHandler):
    """报名通咨询列表"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        add_time = self.get_argument('add_time', '')
        area = yield couchm.get_addr_area_with_phone(phone)
        info = yield couchm.get_pub_consult_list(add_time, area)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class ConsultListHandler(APIRequestHandler):
    """咨询列表"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        add_time = self.get_argument('add_time', '')
        info = yield couchm.get_consult_list(phone, add_time)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class GetMySchoolHandler(APIRequestHandler):
    """我的驾校"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        info = yield couchm.get_my_school(phone)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class SetMySchoolHandler(APIRequestHandler):
    """设置我的驾校"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        area = self.get_argument('area', '')
        school = self.get_argument('school', '')
        yield couchm.set_my_school(phone, area, school)
        res = {"code": "0", "msg": u"设置成功"}
        utiltool.response_write(self, res)


class GetCouchDetailHandler(APIRequestHandler):
    """教练详情"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        couch_phone = self.get_argument('couch_phone', '')
        info = yield couchm.get_couch_detail(phone, couch_phone)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class SearchCouchListHandler(APIRequestHandler):
    """搜索教练列表"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        area = self.get_argument('area', '')
        keyword = self.get_argument('keyword', '')
        longitude = self.get_argument('longitude', '')  # 经度
        latitude = self.get_argument('latitude', '')  # 纬度
        page = self.get_argument('page', '1')  #
        page_count = self.get_argument('page_count', '20')  #
        start = (int(page) - 1) * int(page_count)
        end = int(page_count) * int(page)
        info_all = yield couchm.search_couch_list(area, keyword)
        info = info_all[start: end]
        for item in info:
            if longitude and latitude:
                distance = yield couchm.get_distance_with_phone(item['phone'], longitude, latitude)
            else:
                distance = -1
            item['distance'] = distance
            item['auth'] = yield couchm.get_couch_auth_status(item['phone'])
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class CouchInfoStatusHandler(APIRequestHandler):
    """获取教练名片信息状态"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        info = yield couchm.check_card_info(phone)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class SetCouchSchoolHandler(APIRequestHandler):
    """设置教练个人信息驾校"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        area = self.get_argument('area', '')
        school = self.get_argument('school', '')
        yield couchm.set_couch_school(phone, area, school)
        res = {"code": "0", "msg": u"设置成功"}
        utiltool.response_write(self, res)


class MyCouchHandler(APIRequestHandler):
    """我的教练"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        longitude = self.get_argument('longitude', '')  # 经度
        latitude = self.get_argument('latitude', '')  # 纬度
        info = yield couchm.get_my_couch_info(phone, longitude, latitude)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class GetSharePicHandler(APIRequestHandler):
    """获取分享图片"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        info = yield couchm.get_share_pic()
        info['share_pic'] = settings.IMG_BASE_URL + "system/{}".format(info['share_pic'])
        info['couch_share_pic'] = settings.IMG_BASE_URL + "system/{}".format(info['couch_share_pic'])
        info['bottom_pic'] = settings.IMG_BASE_URL + "system/{}".format(info['bottom_pic'])
        info['couch_bottom_pic'] = settings.IMG_BASE_URL + "system/{}".format(info['couch_bottom_pic'])
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class StudentCntHandler(APIRequestHandler):
    """获取学员数量"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        info = yield couchm.get_student_cnt()
        base_cnt = yield userm.get_base_cnt()
        res = {"code": "0", "info": info + base_cnt['student_base_cnt']}
        utiltool.response_write(self, res)


class CouchCircledHandler(APIRequestHandler):
    """考友圈入口头像"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        info = yield couchm.get_couch_circle_avatar()
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class AddSchoolHandler(APIRequestHandler):
    """用户添加驾校"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        phone = self.get_argument('phone', '')
        area = self.get_argument('area', '')
        school = self.get_argument('school', '')
        current_time = timetool.get_timestamp()
        yield couchm.user_add_driver_school(phone, area, school, current_time)
        res = {"code": "0", "msg": u"添加成功"}
        utiltool.response_write(self, res)


class WechatShareHandler(APIRequestHandler):
    """用户添加驾校"""
    @checktool.check_sign
    @gen.coroutine
    def get(self):
        code = self.get_argument('code', '')
        open_id = yield paym.get_openid_with_code(code)
        print open_id
        phone = ""
        if open_id:
            phone = yield userm.check_wx_bind(open_id)
        phone_code = yield couchm.get_phonecode_with_phone(phone)
        self.redirect("http://app.qcjkjg.com/static/html/invitation_download.html?from=wechat&share_from=wechat&phone_code={}".format(phone_code))
